package com.wht.controller;

import com.wht.domain.Music;
import com.wht.domain.Music_Singer;
import com.wht.service.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author wanghaotian
 * time:2022/11/28,0028 22:08
 * description:
 */

@WebServlet("/UpdateMusicController")
public class UpdateMusicController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String mid = req.getParameter("mid");
        String name = req.getParameter("name");
        String album = req.getParameter("album");
        Music music = new Music(Integer.parseInt(mid),name,album,null);
        String[] sids = req.getParameterValues("sid");
        UpdateMusicService updateMusicService = new UpdateMusicServiceImpl();
        DeleteMusicSingerService deleteMusicSingerService = new DeleteMusicSingerServiceImpl();
        AddMusicSingerService addMusicSingerService = new AddMusicSingerServiceImpl();
        int number = updateMusicService.updateMusic(music);
        if(number>0){
            deleteMusicSingerService.deleteMusicSinger(mid);
            for (String sid : sids) {
                Music_Singer musicSinger = new Music_Singer(Integer.parseInt(mid),Integer.parseInt(sid));
                addMusicSingerService.addMusicSinger(musicSinger);
            }
        }
        resp.sendRedirect("/FindAllMusicController");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}